#include "../main.h"

void initPeerMsg()
{
    InitQueue(&peer_msg_queue);
    pthread_mutex_init(&peer_msg_mutex, NULL);
}

//处理消息
void enter_peerMsg_Queue(char *data, int length, int id)
{
    if (length > 0)
    {
        pthread_mutex_lock(&peer_msg_mutex);
        EnQueue(&peer_msg_queue, data, length, id);
        // OUT_DEBUG(DEBUG,"peer queue size：%d", peer_msg_queue.size);
        free(data);
        data = NULL;
        pthread_mutex_unlock(&peer_msg_mutex);
    }
    threadpool_add_job(pool, process_peerMsg_Queue, &peer_msg_queue);
}
//处理peer消息
void *process_peerMsg_Queue(void *arg)
{
    Queue *c_msgQueue = (Queue *)arg;
    Msgbody *msg = DeQueue(c_msgQueue);
    if (msg != NULL)
    {
        if (msg != NULL)
        {
            if (msg->length > 0)
            {
                parse_msg(msg->id, msg->data, msg->length);
                //tcp_socket_write(msg->id, msg->data, msg->length);
            }
            free(msg->data);
            msg->data = NULL;
            free(msg);
            msg = NULL;
        }
    }
    else
    {
        OUT_DEBUG(DEBUG, "peer msg body not exist\n");
    }
    return NULL;
}